<?php

class Edit_profil extends Controller {

    function Edit_profil()
    {
        parent::Controller();
        $this->load->helper('directory');

        if(!$this->session->userdata('id_admin'))
            redirect('admin/login');
    }

    function index()
    {
        // jangan ke sini kalo belom login
        if(!$this->session->userdata('id_admin'))
            redirect('admin/login');

        // kalo form disubmit
        if ($this->input->post('submit'))
        {
            // validasi dulu

            $this->load->library('form_validation');

            $this->form_validation->set_rules('input_nama', 'Nama Admin', 'trim|required');
            $this->form_validation->set_rules('input_email', 'Email', 'trim|required|valid_email');
            $this->form_validation->set_rules('input_telp', 'No Telp', 'trim|required');
            $this->form_validation->set_rules('input_usia', 'Usia', 'trim|required');
            $this->form_validation->set_rules('input_tempatlahir', 'Tempat Lahir', 'trim|required');
            $this->form_validation->set_rules('input_tgllahir', 'Tanggal Lahir', 'trim|required');
            $this->form_validation->set_rules('input_alamat', 'Alamat', 'trim|required');

            if ($this->form_validation->run() == FALSE)
            {
                // on error
            }
            else
            {
                // on success: update

                $data_profil = array(
                    'nama_admin' => $this->input->post('input_nama'),
                    'email' => $this->input->post('input_email'),
                    'telp' => $this->input->post('input_telp'),
                    'usia' => $this->input->post('input_usia'),
                    'tempat_lahir' => $this->input->post('input_tempatlahir'),
                    'tgl_lahir' => $this->input->post('input_tgllahir'),
                    'alamat' => $this->input->post('input_alamat'),
                );

                $this->db->where('id_admin', $this->session->userdata('id_admin'));
                $this->db->update('tb_admin', $data_profil);

                $data['updated'] = true;
            }
        }

        // ambil data user buat diedit
        $this->db->where('id_admin', $this->session->userdata('id_admin'));
        $q = $this->db->get('tb_admin');
        $data['user_data'] = $q->row_array();

        $data['judul'] = "Edit Profil Saya";
        $data['template_konten'] = 'admin/admin_edit_profil';

        $this->load->vars($data);
        $this->load->view('/admin/template');
    }

    
}
